﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using System.Data.SqlClient;
using System.Data.OleDb;
using KPIS.GERP.GBMS.MODEL;
using KPIS.GERP.GBMS.DAL;

namespace KPIS.GERP.GBMS.DAL
{
    public sealed class RequestExpenseDAL //: IProjectRequestExpenseDAL
    {
       
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public RequestExpenseDAL()
        {

        }
       
        public int InsertExpense(int reqSeq, int exp, int user)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@reqSeq", reqSeq);
                dbManager.AddParameters(1, "@exp", exp);
                dbManager.AddParameters(2, "@status", SystemInfo._WorkRecordStatus);
                dbManager.AddParameters(3, "@user", user);


                strSQL = "INSERT INTO BGM_REQUEST_EXPENSE(BGM_REQ_SEQ, BGM_EXP_SEQ, BGM_SOURCE_SEQ, RECORD_STATUS, CREATED_BY)"
                    + " VALUES(@reqSeq, @exp, (select eg.bgm_source_seq from bgm_mas_expense e inner join bgm_mas_expense_group eg on e.bgm_exp_grp_seq = eg.bgm_exp_grp_seq where e.bgm_exp_seq = @exp), @status, @user)";

                return dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                return 0;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public int UpdateExpense(int reqExpSeq, string reqExpDescr, double reqExpPrice, int reqExpQty
        , double reqExpMtef1, double reqExpMtef2, double reqExpMtef3, string reqExpResource)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(9);

                dbManager.AddParameters(0, "@reqExpSeq", reqExpSeq);
                dbManager.AddParameters(1, "@reqExpDescr", reqExpDescr);
                dbManager.AddParameters(2, "@reqExpPrice", reqExpPrice);
                dbManager.AddParameters(3, "@reqExpQty", reqExpQty);
                dbManager.AddParameters(4, "@reqExpMtef1", reqExpMtef1);
                dbManager.AddParameters(5, "@reqExpMtef2", reqExpMtef2);
                dbManager.AddParameters(6, "@reqExpMtef3", reqExpMtef3);
                dbManager.AddParameters(7, "@reqExpResource", reqExpResource);
                dbManager.AddParameters(8, "@status", SystemInfo._WorkRecordStatus);

                strSQL = "UPDATE BGM_REQUEST_EXPENSE"
                    + " SET BGM_REQ_EXP_DESCR = @reqExpDescr"
                        + ", BGM_REQ_EXP_PRICE = @reqExpPrice"
                        + ", BGM_REQ_EXP_QTY = @reqExpQty"
                        + ", BGM_REQ_EXP_MTEF1 = @reqExpMtef1"
                        + ", BGM_REQ_EXP_MTEF2 = @reqExpMtef2"
                        + ", BGM_REQ_EXP_MTEF3 = @reqExpMtef3"
                        + ", ICM_RESOURCE_LIST_SEQ = @reqExpResource"
                        + ", RECORD_STATUS = @status"
                        + ", UPDATED_WHEn = GetDate()"
                    + " WHERE BGM_REQ_EXP_SEQ = @reqExpSeq";

                return dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                return 0;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public int DeleteExpense(int seq, int updated_by)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@seq", seq);
                dbManager.AddParameters(1, "@record_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@updated_by", updated_by);

                strSQL = "UPDATE BGM_REQUEST_EXPENSE"
                    + " SET RECORD_STATUS = @record_status"
                        + ", UPDATED_BY = @updated_by"
                        + ", UPDATED_WHEN = GetDate()"
                    + " WHERE BGM_REQ_EXP_SEQ = @seq";

                return dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                return 0;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
